/**
 * 
 */
package com.changil.admin.protocol;

import org.json.JSONException;
import org.json.JSONObject;

import com.changil.admin.dto.ArticleList;
import com.changil.admin.dto.BooleanBean;
import com.changil.admin.dto.InsertedId;
import com.changil.admin.dto.User;
import com.changil.admin.protocol.http.request.ArticleListRequest;
import com.changil.admin.protocol.http.request.DeleteArticleRequest;
import com.changil.admin.protocol.http.request.LoginRequest;
import com.changil.admin.protocol.http.request.ModifyArticleRequest;
import com.changil.admin.protocol.http.request.WriteArticleRequest;
import com.changil.admin.protocol.http.response.ArticleListResponse;
import com.changil.admin.protocol.http.response.DeleteArticleResponse;
import com.changil.admin.protocol.http.response.LoginResponse;
import com.changil.admin.protocol.http.response.ModifyArticleResponse;
import com.changil.admin.protocol.http.response.WriteArticleResponse;

/**
 * @author skplanet
 * 
 */
public class ApiManager {
	private static ApiManager sInstance = null;


	public static ApiManager getsInstance() {
		if (sInstance == null) {
			synchronized (ApiManager.class) { 
				if (sInstance == null) { 
					sInstance = new ApiManager();
				}
			}
		}
		return sInstance;
	}
	
	public User login(String id, String password) {
		LoginRequest request = new LoginRequest(id, password);
		LoginResponse httpResponse = new LoginResponse();
		User result = null;
		try{
			result = (User)httpResponse.response(new JSONObject(request.request()));
		} catch(JSONException e) {
			
		}
		return result;
	}
	
	public ArticleList getArticleList(int boardId, int fromId, int count, boolean isDownSide) {
		ArticleListRequest request = new ArticleListRequest(boardId, fromId, count, isDownSide);
		ArticleListResponse httpResponse = new ArticleListResponse();
		ArticleList result = null;
		try{
			result = (ArticleList)httpResponse.response(new JSONObject(request.request()));
		} catch(JSONException e) {
			
		}
		return result;
	}
	
	public InsertedId writeArticle(int boardId, String title, String content) {
		WriteArticleRequest request = new WriteArticleRequest(boardId,title,content);
		WriteArticleResponse httpResponse = new WriteArticleResponse();
		InsertedId result = null;
		try{
			result = (InsertedId)httpResponse.response(new JSONObject(request.request()));
		} catch(JSONException e) {
			
		}
		return result;
	}
	
	public BooleanBean modifyArticle(int id, String title, String content) {
		ModifyArticleRequest request = new ModifyArticleRequest(id,title,content);
		ModifyArticleResponse httpResponse = new ModifyArticleResponse();
		BooleanBean result = null;
		try{
			result = (BooleanBean)httpResponse.response(new JSONObject(request.request()));
		} catch(JSONException e) {
			
		}
		return result;
	}
	
	public BooleanBean deleteArticle(int articleId) {
		DeleteArticleRequest request = new DeleteArticleRequest(articleId);
		DeleteArticleResponse httpResponse = new DeleteArticleResponse();
		BooleanBean result = null;
		try{
			result = (BooleanBean)httpResponse.response(new JSONObject(request.request()));
		} catch(JSONException e) {
			
		}
		return result;
	}
	
	

//	public void getHistoryList(final DataLoadListener<HistoryList> listener, String fromId, int count, boolean isDownSide) {
//		HistoryListRequest request = new HistoryListRequest(fromId, count, isDownSide);
//		request.request(new HttpResponseListener() {
//			
//			@Override
//			public void onSuccess(String response) {
//				HistoryListResponse httpResponse = new HistoryListResponse();
//				listener.onSuccess((HistoryList)httpResponse.response(new JSONObject(response)));
//			}
//			
//			@Override
//			public void onFail() {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//	}
	
//	public void getThisWeekBibleWordList(final DataLoadListener<BibleWordList> listener, String date) {
//		BibleWordListRequest request = new BibleWordListRequest(date);
//		request.request(new JsonHttpResponseHandler("UTF-8") {
//
//			@Override
//			public void onSuccess(JSONObject response) {
//				BibleWordListResponse httpResponse = new BibleWordListResponse();
//				listener.onSuccess((BibleWordList)httpResponse.response(response));
//			}
//
//			@Override
//			public void onFailure(int statusCode, Header[] headers,
//					Throwable throwable, JSONObject errorResponse) {
//				listener.onFail();
//			}
//		});
//	}
//	
//	public void getLastWeekWord(final DataLoadListener<Word> listener, String date) {
//		WordRequest request = new WordRequest(date);
//		request.request(new JsonHttpResponseHandler("UTF-8") {
//
//			@Override
//			public void onSuccess(JSONObject response) {
//				WordResponse httpResponse = new WordResponse();
//				listener.onSuccess((Word)httpResponse.response(response));
//			}
//
//			@Override
//			public void onFailure(int statusCode, Header[] headers,
//					Throwable throwable, JSONObject errorResponse) {
//				listener.onFail();
//			}
//		});
//	}
//	
//	public void getGracefulWord(final DataLoadListener<GracefulWord> listener, String date) {
//		GracefulWordRequest request = new GracefulWordRequest(date);
//		request.request(new JsonHttpResponseHandler("UTF-8") {
//
//			@Override
//			public void onSuccess(JSONObject response) {
//				GracefulWordResponse httpResponse = new GracefulWordResponse();
//				listener.onSuccess((GracefulWord)httpResponse.response(response));
//			}
//
//			@Override
//			public void onFailure(int statusCode, Header[] headers,
//					Throwable throwable, JSONObject errorResponse) {
//				listener.onFail();
//			}
//		});
//	}

}
